package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Bom;
import com.efast.dmom.mes.bean.BomItem;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.BomDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.BomEntity;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class BomDataHandler extends PartRelateDataHandler<Bom>{

	@Autowired
	private BomDao bomDao;
	@Autowired
	private BomItemDataHandler bomItemHandler;
	
	@Override
	public ResultBean save(Bom obj) {
		ResultBean resultBean =new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			bomDao.save(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean update(Bom obj) {
		ResultBean resultBean =new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			bomDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	@Override
	public ResultBean delete(Bom obj) {
		ResultBean resultBean =new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			bomDao.delete(obj.getInnerData());
		}
		return resultBean;
	}

	public Bom getById(long id) {
		Bom bom = new Bom();
		bom.setInnerData(bomDao.getById(id));
		return bom;
	}

	public Bom getByName(String name) {
		Bom bom = new Bom();
		bom.setInnerData(bomDao.getByName(name));
		return bom;
	}

	public Bom getByNameAndRevision(String bomName, String bomRevision) {
		Bom bom = new Bom();
		BomEntity bomEntity = bomDao.getBomByNameAndRevision(bomName, bomRevision);
		if(bomEntity!=null) {
			bom.setInnerData(bomEntity);
			List<BomItem> byBomId = bomItemHandler.getByBomId(bomEntity.getId());
			if(byBomId!=null && byBomId.size()>0) {
				bom.setItems(byBomId);
			}
		}
		return bom;
	}

	public List<Bom> getAllBom() {
		List<BomEntity> entityList = bomDao.getAllBom();
		List<Bom> bomList = new ArrayList<Bom>();
		if (entityList != null && entityList.size() > 0) {
			for (BomEntity entityData : entityList) {
				Bom bom = new Bom();
				bom.setInnerData(entityData);
				List<BomItem> byBomId = bomItemHandler.getByBomId(entityData.getId());
				if(byBomId!=null && byBomId.size()>0) {
					bom.setItems(byBomId);
				}
				bomList.add(bom);
			}
			return bomList;
		}
		return null;
	}

	public List<BomEntity> getAllBomAndBomItem(Bom bomData,int start,int length) {
		List<BomEntity> entityList = bomDao.getAllBomAndBomItem(bomData.getInnerData(),start,length);
		if (entityList != null && entityList.size() > 0) {
			return entityList;
		}
		return null;
	}

	@Override
	public ResultBean saveBatch(List<Bom> obj) {
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Bom> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Bom> obj) {
		// TODO Auto-generated method stub
		return null;
	}

}
